//
//  LXMineCellView.m
//  cat
//
//  Created by imera on 16/3/11.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXMineCellView.h"

#define LXMineCellViewMargin 10

@interface LXMineCellView()

@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, weak) UILabel *titleLabel;

/** 箭头 */
@property (nonatomic, weak) UIImageView *arrowImageView;

/** 标题 */
@property (nonatomic, strong) NSString *title;

/** 图标 */
@property (nonatomic, strong) NSString *image;

@end

@implementation LXMineCellView

+ (instancetype)initWithTitle:(NSString *)title image:(NSString *)image
{
    return [[self alloc]initWithTitle:title image:image];
}

- (instancetype)initWithTitle:(NSString *)title image:(NSString *)image
{
    self.title = title;
    self.image = image;
    return [self init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        [self initSubViews];
    }
    return self;
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    UIImageView *imageView = [[UIImageView alloc]init];
    self.imageView = imageView;
    [self addSubview:imageView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    self.titleLabel = titleLabel;
    [self addSubview:titleLabel];
    
    UIImageView *arrowImageView = [[UIImageView alloc]init];
    self.arrowImageView = arrowImageView;
    [self addSubview:arrowImageView];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //图标
    UIImageView *imageView = self.imageView;
    imageView.image = [UIImage imageNamed:self.image];
    
    CGFloat imageViewX = LXMineCellViewMargin;
    CGFloat imageViewW = 20;
    CGFloat imageViewH = imageViewW;
    CGFloat imageViewY = (self.frame.size.height - imageViewH) / 2;
    imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    
    //标题
    UILabel *titleLabel = self.titleLabel;
    titleLabel.text = self.title;
    titleLabel.font = [UIFont systemFontOfSize:13];
    
    CGSize titleLabelSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:titleLabel.font}];
    CGFloat titleLabelW = titleLabelSize.width;
    CGFloat titleLabelH = titleLabelSize.height;
    CGFloat titleLabelX = CGRectGetMaxX(imageView.frame) + 30;
    CGFloat titleLabelY = (self.frame.size.height - titleLabelH) / 2;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    //箭头
    UIImageView *arrowImageView = self.arrowImageView;
    [arrowImageView setImage:[UIImage imageNamed:@"icon_arrow"]];
    CGFloat arrowImageViewW = 20;
    CGFloat arrowImageViewH = 20;
    CGFloat arrowImageViewX = self.frame.size.width - arrowImageViewW - LXMineCellViewMargin;
    CGFloat arrowImageViewY = (self.frame.size.height - arrowImageViewH) / 2;
    arrowImageView.frame = CGRectMake(arrowImageViewX, arrowImageViewY, arrowImageViewW, arrowImageViewH);
    
}

@end
